package edu.base.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.base.util.HigherResponse;
import edu.base.dao.RecRepDao;
import edu.base.entity.RecordRepair;

@Service
public class RecRepServiceImpl implements RecRepService{
	
	@Autowired
	private RecRepDao recRepDao;
	
	//分页查询所有报修记录
	@Override
	public HigherResponse<PageInfo<RecordRepair>> queryRecByPage(Integer pageNum, Integer pageSize) {

		//1.开启分页
		PageHelper.startPage(pageNum,pageSize);
		//2.查询数据
		List<RecordRepair> allRec=recRepDao.queryAllRec();
		//System.out.println("queryRecByPage"+allRec);
		//3.将查询到的数据放入pageInfo
		PageInfo<RecordRepair> pageInfo=new PageInfo<RecordRepair>(allRec);
		return HigherResponse.getSuccessRespon(pageInfo);
	}
	
	//根据日期、预约方式和报修状态查询报修记录
	@Override
	public HigherResponse<PageInfo<RecordRepair>> queryRecByDate(String beginDate, String endDate,
			Integer repairStatus, Integer appointment, Integer pageNum, Integer pageSize) {
		//1.开启分页
		PageHelper.startPage(pageNum,pageSize);
		//2.查询数据
		//System.out.println("beginDate:"+beginDate+"  endDate:"+endDate+"   repair_status:"+ repair_status+"  appointment:"+appointment);
		List<RecordRepair> record=recRepDao.queryRecByDate(beginDate, endDate, repairStatus,appointment);
		//3.将查询到的数据放入pageInfo
		PageInfo<RecordRepair> pageInfo=new PageInfo<RecordRepair>(record);
		if(pageInfo.getSize()==0 || null==pageInfo) {
			return HigherResponse.getFailedRespon("无记录");
		}
		return HigherResponse.getSuccessRespon(pageInfo);	
	}
	
	//根据报修id查询报修记录
	@Override
	public HigherResponse<RecordRepair> queryOneRec(Integer id) {
		RecordRepair record=recRepDao.queryOneRec(id);
		if(null==record) {
			return HigherResponse.getFailedRespon("无记录");
		}
		return HigherResponse.getSuccessRespon(record);
	}
	
	//更新报修记录信息
	@Override
	public HigherResponse<String> updateRecById(RecordRepair recRep) {
		/*
		 * System.out.println(recRep.getId().getClass().toString()+":"+recRep.getId());
		 * System.out.println(recRep.getRepair_time().getClass().toString()+":"+recRep.getRepair_time());
		 */
		boolean flag=recRepDao.updateRecById(recRep);
		if(false==flag) {
			return HigherResponse.getFailedRespon("修改失败");
		}
		return HigherResponse.getSuccessRespon("修改成功");
	}
	
	//添加报修记录
	@Override
	public HigherResponse<String> insertRec(RecordRepair recRep) {
		int flag=recRepDao.insertRec(recRep);
		if(0==flag) {
			return HigherResponse.getFailedRespon("添加记录失败");
		}
		return HigherResponse.getSuccessRespon("添加记录成功");
	}
	
	//删除报修记录for real
	@Override
	public HigherResponse<String> deleteRecRepById(Integer id) {
		boolean flag=recRepDao.deleteRecRepById(id);
		if(false==flag) {
			return HigherResponse.getFailedRespon("删除失败");
		}
		return HigherResponse.getSuccessRespon("删除成功");
	}
	
	//删除报修记录fake
	@Override
	public HigherResponse<String> updateStatus(Integer id) {
		boolean flag=recRepDao.updateStatus(id, 2);
		if(false==flag) {
			return HigherResponse.getFailedRespon("删除失败");
		}
		return HigherResponse.getSuccessRespon("删除成功");
	}
	
	//批量删除数据
	@Override
	public HigherResponse<String> updateRecStatus(List<Integer> ids) {
		boolean flag=recRepDao.updateRecStatus(ids, 2);
		if(false==flag) {
			return HigherResponse.getFailedRespon("删除失败");
		}
		return HigherResponse.getSuccessRespon("删除成功");
	}

}
